<?php

namespace app\common\model;

use addons\xykeep\exception\Exception;
use app\api\model\xykeep\User;

use think\Config;
use think\Model;
use traits\model\SoftDelete;
use think\Db;

class StaffOrder extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'staff_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'pay_type_text',
        'paytime_text',
        'platform_text',
        'status_text'
    ];



    // 订单详情
    public static function getDetail($params)
    {
        $user = User::info();
        extract($params);

        $order = self::where('user_id', $user->id);

        if (isset($order_sn)) {
            $order = $order->where('order_sn', $order_sn);
        }
        if (isset($id)) {
            $order = $order->where('id', $id);
        }

        $order = $order->find();

        if (!$order) {
            new Exception('订单不存在');
        }

        return $order;
    }

    /**
     * 添加订单
     */
    public static function addOrder($params)
    {
        extract($params);

        $total_fee = Config::get('site.accept_job_deposit');

        $user = User::info();

        $close_time = 15; //15分钟为支付自动关闭

        $orderData = [];
        $orderData['job_id'] = $job_id;


        $orderData['order_sn'] = xykeepCreateOrderNo();
        $orderData['user_id'] = $user->id;
        $orderData['total_amount'] = $total_fee;
        $orderData['total_fee'] = $total_fee;
        $orderData['ext'] = json_encode([
            'expired_time' => time() + ($close_time * 60),
        ]);

        $order = self::create($orderData);

        return $order;
    }

    /**
     * 订单支付成功
     *
     * @param [type] $order
     * @param [type] $notify
     * @return void
     */
    public function paySuccess($order, $notify)
    {
        $_time = time();
        $order->status = 1;
        $order->paytime = $_time;
        $order->transaction_id = $notify['transaction_id'];
        $order->payment_json = $notify['payment_json'];
        $order->pay_type = $notify['pay_type'];
        $order->pay_fee = $notify['pay_fee'];
        $order->save();

        return $order;
    }




    public function getPayTypeList()
    {
        return ['wechat' => __('Wechat'), 'balance' => __('Balance')];
    }

    public function getPlatformList()
    {
        return ['wxMiniProgram' => __('Platform wxMiniProgram'), 'wxOfficialAccount' => __('Platform wxOfficialAccount')];
    }

    public function getStatusList()
    {
        return ['-2' => __('Status -2'), '-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPlatformTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['platform']) ? $data['platform'] : '');
        $list = $this->getPlatformList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('\app\admin\model\xykeep\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }

    public function job()
    {
        return $this->belongsTo('\app\common\model\TeacherJob', 'job_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }


}
